package com.cogent.business.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

import jxl.Sheet;
import jxl.Workbook;

import com.cogent.business.action.AbstractAction;
import com.cogent.common.io.FileUtil;
import com.cogent.common.util.ExecelUtil;
import com.cogent.common.util.MapUtil;

public class PopWindowAction extends AbstractAction {
	private File upload; 
	
	public String execute() {
		HashMap formMap = getFormMap();
		setResponseMsg("formmap", formMap);
		return "success";
	}
	
	public String code() {
		HashMap formMap = getFormMapWithUser();
		if(upload != null) {
			try {
				InputStream is1 = new FileInputStream(upload);
				jxl.Workbook rwb1 = Workbook.getWorkbook(is1); 
				Sheet sheet = rwb1.getSheet(0);
				int rows=sheet.getRows();
				StringBuffer str = new StringBuffer();
				for(int d=0; d<rows; d++)
					str.append(ExecelUtil.getCellValue(sheet, 0, d)).append(";");
				String temp = MapUtil.getStringValue(formMap, "goodscode").replace("\r\n", ";");
				String curtemp = str.toString();
				if(curtemp.length() > 0) {
					curtemp = curtemp.substring(0, curtemp.length() - 1);
					formMap.put("goodscode", temp + ";" + curtemp);
				}
				rwb1.close();
				is1.close();
				FileUtil.deleteFile(upload);
			} catch (Exception e) {
				formMap.put("err_msg", "导入串码过程中出现异常，请按规定格式导入串码!");
			}			
		}
		setResponseMsg("formmap", formMap);
		
		return "code";
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}
}
